<script setup>
import NavBarHeader from '@/components/NavBarHeader.vue'
import { ref } from 'vue'
import axiosInstance from '@/utils/request'
import { useUserStore } from '@/stores/user'
import router from '@/router';
const isSearch = ref(false)
const searchValue = ref('')
const searchResults = ref([])
const store = useUserStore()
const onSearch = (val) => showToast(val)
const onClickButton = () => {
  axiosInstance
    .get('/user/search', {
      headers: {
        token: store.token
      },
      params: {
        keyWords: searchValue.value
      }
    })
    .then((response) => {
      isSearch.value = true
      searchResults.value = response.data.data
    })
    .catch((error) => {
      console.log(error)
    })
}
const handleContactDetail = (searchResult) => {
  router.push(`/contact/${searchResult.id}`)
}
</script>

<template>
  <NavBarHeader>
    <template #title>
      <span style="font-weight: 700">搜索联系人</span>
    </template>
  </NavBarHeader>
  <van-search
    v-model="searchValue"
    show-action
    placeholder="请输入搜索关键词"
    @search="onSearch"
  >
    <template #action>
      <div @click="onClickButton">搜索</div>
    </template>
  </van-search>
  <div class="contact-list" v-if="searchResults.length > 0">
    <ul>
      <li
        v-for="searchResult in searchResults"
        :key="searchResult.id"
        class="contact-item"
      >
        <img :src="searchResult.avatar" alt="头像" class="avatar" />
        <div class="contact-info" @click="handleContactDetail(searchResult)">
          <p class="nickname">{{ searchResult.nickname }}</p>
        </div>
      </li>
    </ul>
  </div>
  <div v-else-if="isSearch && searchResults.length === 0">
    <van-empty description="暂无找到该联系人" />
  </div>
</template>

<style scoped>
.contact-list {
  padding: 10px;
}

.contact-item {
  display: flex;
  align-items: center;
  padding: 10px;
  margin-bottom: 10px; /* 每个联系人之间的间距 */
  border-radius: 8px; /* 圆角 */
  background-color: #fff; /* 背景颜色 */
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1); /* 阴影效果 */
  transition: all 0.3s ease; /* 过渡效果 */
}
.contact-item:hover {
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2); /* 悬停时的阴影效果 */
}

.avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  margin-right: 10px;
}

.contact-info {
  flex-grow: 1; /* 使信息部分占据可用空间 */
}

.nickname {
  font-size: 16px;
  font-weight: bold;
  color: #000;
}
</style>
